const _ = require('lodash')

// 生成 1-49 的数组
const all_numbers = Array.from({ length: 49 }, (_, i) => i + 1).map((num) => (num < 10 ? `0${num}` : `${num}`))

let emptyCount = 0

const getLucky = (resultArray, currentHistoryItem, numberGroupMap, flatGroupMap) => {
  // 用 Set 来避免重复的号码
  const killSet = new Set()
  const kill_group_names = []

  // const top3_group_array = _.chain(resultArray)
  //   .take(3)
  //   .map((group) => {
  //     const { groupName } = group
  //     const index = numberGroupMap[currentHistoryItem.number][groupName]
  //     const sub_array = flatGroupMap[groupName][index]
  //     return {
  //       groupName,
  //       sub_array
  //     }
  //   })
  //   .value()
  // if (!top3_group_array.length) {
  //   console.log('!top3_group_array.length', top3_group_array.length)
  // }

  resultArray.forEach((group, i) => {
    const { groupName } = group
    const index = numberGroupMap[currentHistoryItem.number][groupName]
    const sub_array = flatGroupMap[groupName][index]

    sub_array.forEach((number) => {
      if (killSet.size < 16) {
        killSet.add(number)
        if (!kill_group_names.includes(groupName)) {
          kill_group_names.push(groupName)
        }
      }
    })
  })

  if (killSet.size < 16) {
    // console.log('killSet.size', killSet.size)
    // console.log('emptyCount', ++emptyCount)
    return {
      lucky_array: [],
      kill_array: [],
      kill_group_names: [],
      // top3_group_array: []
    }
  } else {
    const uniqueKillArray = [...killSet]

    // 幸运号码
    const kill_array = uniqueKillArray.map((item) => item.toString().padStart(2, '0'))

    // 杀号
    const lucky_array = all_numbers.filter((numberStr) => !kill_array.includes(numberStr))

    return {
      lucky_array,
      kill_array,
      kill_group_names,
      // top3_group_array
    }
  }
}

module.exports = getLucky
